package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.ConfigFeeItem;
import com.ruoyi.system.domain.PayReminders;
import com.ruoyi.system.domain.vo.FeeContractVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PaymentContractMapper;
import com.ruoyi.system.domain.PaymentContract;
import com.ruoyi.system.service.IPaymentContractService;

/**
 * 租售合同管理Service业务层处理
 * 
 * @author fcs
 * @date 2024-02-29
 */
@Service
public class PaymentContractServiceImpl implements IPaymentContractService 
{
    @Autowired
    private PaymentContractMapper paymentContractMapper;

    /**
     * 查询租售合同管理
     * 
     * @param id 租售合同管理主键
     * @return 租售合同管理
     */
    @Override
    public PaymentContract selectPaymentContractById(String id)
    {
        return paymentContractMapper.selectPaymentContractById(id);
    }

    /**
     * 查询租售合同管理列表
     * 
     * @param paymentContract 租售合同管理
     * @return 租售合同管理
     */
    @Override
    public List<PaymentContract> selectPaymentContractList(PaymentContract paymentContract)
    {
        List<PaymentContract> paymentContracts = paymentContractMapper.selectPaymentContractList(paymentContract);
        return paymentContracts;
    }

    /**
     * 新增租售合同管理
     * 
     * @param paymentContract 租售合同管理
     * @return 结果
     */
    @Override
    public int insertPaymentContract(PaymentContract paymentContract)
    {
        return paymentContractMapper.insertPaymentContract(paymentContract);
    }

    /**
     * 修改租售合同管理
     * 
     * @param paymentContract 租售合同管理
     * @return 结果
     */
    @Override
    public int updatePaymentContract(PaymentContract paymentContract)
    {
        paymentContract.setResourceType(String.valueOf(Integer.parseInt(paymentContract.getContractType()) + 1));
        // 修改合同前先清空原有收费项
        paymentContractMapper.deletePayRemind(paymentContract.getResourceId());
        // 修改合同信息
        int i = paymentContractMapper.updatePaymentContract(paymentContract);
        paymentContractMapper.updatePayment(paymentContract);

        return i;
    }

    /**
     * 批量删除租售合同管理
     * 
     * @param ids 需要删除的租售合同管理主键
     * @return 结果
     */
    @Override
    public int deletePaymentContractByIds(String[] ids)
    {
        return paymentContractMapper.deletePaymentContractByIds(ids);
    }

    /**
     * 删除租售合同管理信息
     * 
     * @param id 租售合同管理主键
     * @return 结果
     */
    @Override
    public int deletePaymentContractById(String id)
    {
        PaymentContract contract = paymentContractMapper.selectPaymentContractById(id);
        String resourceId = contract.getResourceId();
        // 根据商铺id查询撤销合同下是否有未缴费项
        int num = paymentContractMapper.selectPayRemindersById(resourceId);
        if (num == 0) {
            PaymentContract paymentContract = paymentContractMapper.selectPaymentContractById(id);
            PaymentContract payment = new PaymentContract();
            payment.setId(id);
            payment.setBlockCode(paymentContract.getBlockCode());
            payment.setResourceId(resourceId);
            payment.setResourceName(paymentContract.getResourceName());
            payment.setResourceType("3");
            paymentContractMapper.deletePaymentContractById(id);
            paymentContractMapper.updatePayment(payment);
            return paymentContractMapper.insertPaymentContract(payment);
        }
        return -1;
    }

    /**
     * 根据合同收费项列表插入合同收费项数据
     * @param feeContractVo
     * @return
     */
    @Override
    public int insertPayReminders(FeeContractVo feeContractVo) {

        PayReminders pay = new PayReminders();

        int i = 1;
        if (feeContractVo.getFeeList() != null)
        {
            for (ConfigFeeItem fee : feeContractVo.getFeeList()){
                pay.setFeeItemId(fee.getId());
                pay.setFeeName(fee.getName());
                pay.setShopId(feeContractVo.getShopId());
                pay.setOwnerId(feeContractVo.getOwnerId());
                pay.setStarttime(feeContractVo.getStartTime());
                pay.setEndtime(feeContractVo.getEndTime());
                pay.setDeadtime(feeContractVo.getEndTime());
                pay.setPayAmount(feeContractVo.getPrice());
                i += paymentContractMapper.insertPayReminders(pay);
            }
        }
        return i;
    }
}
